<?php
namespace BoxEdu\Hooks;
class SubjectOrderHooks extends \HooksController {

	//优惠金额,代理码
	function order_amount_dec_subject(&$param) {
		$subjectid = $param['subjectid'];
		$order_amount = $param['order_amount']; //'school_num','agent_num'

		//使用学习卡
		if ($study_card_num = $param['study_card_num']) {
			$model = BoxModel("edu_study_cards_user");
			$study_card = $model->where(array('user_id' => $param['user_id'], 'card_code' => $study_card_num))->find();
			if ($study_card['status'] == 1) {
				$model->startTrans();
				$r = $model->where(array('user_id' => $param['user_id'], 'card_code' => $study_card_num))->save(array('status' => 2));
				if ($r) {
					$order_amount = 0;
					$model->commit();
				} else {
					$model->rollback();
				}
			}
		}
		//使用学习卡结束

		$r = Logic("BoxEdu", "User")->check_card($param['school_num'], 'school_num', $subjectid, "buy");
		//	dump($r);exit();
		if ($r['error'] == 0 && $order_amount > 0) {
			$money = $r["money"] > 0 ? $r["money"] : 0;
			$param['school_num_money'] = $money;
			$order_amount -= $money;
		}

		// $r = Logic("BoxEdu", "User")->check_card($data['agent_num'], 'agent_num', $subjectid, "buy");
		//        if ($r['error'] == 0 && $order_amount > 0) {
		//            $money = $r["money"] > 0 ? $r["money"] : 0;
		//            $param['agent_num_money'] = $money;
		//            $order_amount -= $money;
		//        }
		$param['order_amount'] = $order_amount > 0 ? $order_amount : 0;
	}

	//课程订单
	function order_after_make_subject(&$param) {
		$model = BoxModel("edu_subject_order");
		$row = $model->create($param);
		$model->add($row);
		/*******在通在线报名****/
		if ($examination_id = $param['examination_id']) {
			BoxModel("edu_apply_examination")->where(array('id' => $examination_id))->save(array('order_sn' => $param['order_sn']));
		}
	}

	//支付成功
	function order_paySuc_subject(&$param) {
		$order_sn = $param['order_sn'];
		Logic("BoxEdu", "User")->buy_subject_sucess($order_sn);
		//报名状态改变
		BoxModel("edu_apply_examination")->where(array('order_sn' => $order_sn))->save(array('status' => 2));
	}

	//加载
	function order_load_subject(&$param) {
		$order_sn = $param['order_sn'];
		$data = BoxModel("edu_subject_order")->where(array('order_sn' => $order_sn))->find();
		$data['subject'] = BoxModel("edu_subject")->load($data['subjectid']);
		$param = array_merge($param, (array) $data);
	}
	//发货
	function order_logistics_subject(&$param) {
		$order_sn = $param['order_sn'];
		$r = BoxModel("edu_subject_order")->where(array('order_sn' => $order_sn))->save(array('logistics' => $param['logistics']['logistics_num']));
		$data = BoxModel("edu_subject_order")->where(array('order_sn' => $order_sn))->find();
		$subject_info = BoxModel("edu_subject")->where(array('subjectid' => $data['subjectid']))->find();
		if (1) {
			$NotiParams = array(
				'user_id' => $param['logistics']['user_id'],
				'title' => $subject_info['subject'],
				'logistics_name' => $param['logistics']['logistics_name'],
				'logistics_num' => $param['logistics']['logistics_num'],
			);
			Logic("BoxSns", "Noti")->send("00009", $NotiParams);
		}

	}

	//取消
	function order_cancel_subject(&$param) {
		BoxModel("edu_subject_order")->cancel($param['order_sn']);
	}

}
